package cn.raven.pet.service.impl;

import cn.raven.basic.exception.BusinessException;
import cn.raven.basic.utils.BaiduAuditUtils;
import cn.raven.basic.utils.CodeGenerateUtils;
import cn.raven.basic.utils.DistanceUtil;
import cn.raven.basic.utils.Point;
import cn.raven.order.domain.OrderPetAcquisition;
import cn.raven.order.mapper.OrderPetAcquisitionMapper;
import cn.raven.org.domain.Shop;
import cn.raven.org.mapper.ShopMapper;
import cn.raven.pet.domain.Pet;
import cn.raven.pet.domain.SearchMasterMsg;
import cn.raven.pet.domain.SearchMasterMsgAuditLog;
import cn.raven.pet.dto.HandlerMsgDto;
import cn.raven.pet.mapper.PetMapper;
import cn.raven.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.raven.pet.mapper.SearchMasterMsgMapper;
import cn.raven.pet.service.ISearchMasterMsgService;
import cn.raven.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Raven
 * @since 2022-07-19
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private PetMapper petMapper;
    
    /** 
    * @Title: publish
    * @Description: 发布寻主消息
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/19 18:23
    * @Parameters: [searchMasterMsg]
    * @Return void
      */
    @Override
    public void publish(SearchMasterMsg searchMasterMsg) {
        //不过审核是否通过，我们先保存，只不过状态不同而已
        searchMasterMsgMapper.add(searchMasterMsg);
        SearchMasterMsgAuditLog msgAuditLog = new SearchMasterMsgAuditLog();

        if (StringUtils.isEmpty(searchMasterMsg.getName()) ||
                StringUtils.isEmpty(searchMasterMsg.getAddress()) ||
                StringUtils.isEmpty(searchMasterMsg.getCoatColor()) ||
                StringUtils.isEmpty(searchMasterMsg.getTitle())){
            searchMasterMsg.setState(0);
            msgAuditLog.setState(0);
            msgAuditLog.setMsgId(searchMasterMsg.getId());
            msgAuditLog.setAuditTime(new Date());
            msgAuditLog.setNote("必要参数不能为空");
        }
        //获取文字信息
        StringBuffer buffer = new StringBuffer()
                .append(searchMasterMsg.getName())
                .append(searchMasterMsg.getAddress())
                .append(searchMasterMsg.getCoatColor())
                .append(searchMasterMsg.getTitle());
        //百度AI判断是否违规
        if (!BaiduAuditUtils.TextCensor(buffer.toString())){
            searchMasterMsg.setState(0);
            msgAuditLog.setState(0);
            msgAuditLog.setMsgId(searchMasterMsg.getId());
            msgAuditLog.setAuditTime(new Date());
            msgAuditLog.setNote("必要参数违规");
        }else {
            //不违规表示审核通过
            //查询所有店铺，找到最近50公里的
            List<Shop> shops = shopMapper.findAll();
            //使用工具类得到用户地址
            Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
            //在找50公里内的店铺
            Shop shop = DistanceUtil.getNearestShop(point, shops);
            //判断店铺是否存在
            if (shop != null){
                searchMasterMsg.setShopId(shop.getId());
            }else {            //当shopId为null时，表示没有店铺接单，并且是进入寻主池
                msgAuditLog.setState(1);
                msgAuditLog.setMsgId(searchMasterMsg.getId());
                msgAuditLog.setAuditTime(new Date());
                msgAuditLog.setNote("没有商家接单，放入寻主池");
            }
        }
        if (msgAuditLog.getMsgId() != null){
            searchMasterMsgAuditLogMapper.add(msgAuditLog);
        }
        searchMasterMsgMapper.update(searchMasterMsg);
    }

    /**
    * @Title: accept
    * @Description: 宠物待处理接单
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/20 17:13
    * @Parameters: [handlerMsgDto]
    * @Return void
      */
    @Override
    public void accept(HandlerMsgDto handlerMsgDto) {
        if (StringUtils.isEmpty(handlerMsgDto.getHandler()) ||
                StringUtils.isEmpty(handlerMsgDto.getNote())){
            throw new BusinessException("必要参数不能为空");
        }
        //根据msgId去订单表查询是否存在
        OrderPetAcquisition orderPetAcquisition =  orderPetAcquisitionMapper.findOrderExists(handlerMsgDto.getMsgId());
        if (orderPetAcquisition != null){
            throw new BusinessException("订单已被掠夺,你太菜了");
        }
        //查询寻主信息
        SearchMasterMsg msg = searchMasterMsgMapper.findOne(handlerMsgDto.getMsgId());
        //在初始化pet宠物信息
        Pet pet = msg2Pet(msg);
        petMapper.add(pet);
        //初始化订单
        orderPetAcquisition = initOrderPetAcquisition(handlerMsgDto,pet,msg);
        orderPetAcquisitionMapper.add(orderPetAcquisition);
    }
    
    /** 
    * @Title: reject
    * @Description: 拒单
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/20 18:50  
    * @Parameters: [id]
    * @Return void
      */
    @Override
    public void reject(Long id) {
        SearchMasterMsg msg = searchMasterMsgMapper.findOne(id);

        if (msg == null){
            throw new BusinessException("都没有这个订单你拒绝个屁");
        }
        msg.setShopId(null);
        searchMasterMsgMapper.update(msg);
    }

    private OrderPetAcquisition initOrderPetAcquisition(HandlerMsgDto handlerMsgDto, Pet pet, SearchMasterMsg msg) {
        OrderPetAcquisition orderPetAcquisition = new OrderPetAcquisition();
        //使用工具类生成唯一订单号
        String orderSn = CodeGenerateUtils.generateOrderSn(msg.getUserId());
        //设置订单号
        orderPetAcquisition.setOrderSn(orderSn);
        //设置备注
        orderPetAcquisition.setDigest(handlerMsgDto.getNote());
        //设置最迟处理时间
        orderPetAcquisition.setLastcomfirmtime(new Date(System.currentTimeMillis() + 2*24*60*60*1000));
        //收购成功再来修改最终价格
        orderPetAcquisition.setPrice(msg.getPrice());
        orderPetAcquisition.setPetId(pet.getId());
        orderPetAcquisition.setUserId(msg.getUserId());
        orderPetAcquisition.setShopId(msg.getShopId());
        orderPetAcquisition.setAddress(msg.getAddress());
        orderPetAcquisition.setEmpId(handlerMsgDto.getHandler());
        orderPetAcquisition.setSearchMasterMsgId(msg.getId());
        return orderPetAcquisition;

    }

    private Pet msg2Pet(SearchMasterMsg msg) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(msg.getPrice());
        pet.setTypeId(msg.getPetType());
        pet.setShopId(msg.getShopId());
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }
}
